package cn.regexp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopSearchHistory;
import cn.regexp.domain.ShopUser;
import cn.regexp.mapper.SearchHistoryMapper;
import cn.regexp.redis.service.impl.ShopUserRedisServiceImpl;
import cn.regexp.service.ISearchHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryMapper, ShopSearchHistory> implements ISearchHistoryService {
    @Autowired
    private ShopUserRedisServiceImpl shopUserRedisService;
    @Override
    public void saveSearchHistory(String keyword, String token) {
        // 将搜索历史存到数据库中
        ShopSearchHistory searchHistory = new ShopSearchHistory();
        searchHistory.setSearchTime(new Date());
        searchHistory.setIsDel(ShopSearchHistory.NO);
        searchHistory.setKeyword(keyword);
        ShopUser shopUser = shopUserRedisService.getUserByToken(token);
        if (ObjectUtil.isNotEmpty(shopUser)) {
            searchHistory.setUserId(shopUser.getId());
        }
        super.save(searchHistory);
    }
}
